package com.huiyeji.hui.exception;

import com.huiyeji.hui.utils.ErrorEnum;
import com.huiyeji.hui.utils.HuiResponse;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zhaoduo
 */
@RestControllerAdvice(annotations = {RestController.class,Controller.class})
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public HuiResponse<?> handler(Exception exception){
        exception.printStackTrace();
        return HuiResponse.error(exception);
    }

    @ExceptionHandler(HuiException.class)
    public HuiResponse<?> huiHandler(HuiException e){
        return HuiResponse.error(e);
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public HuiResponse<?> duplicateKeyExceptionHandler(DuplicateKeyException e){
        return HuiResponse.error(ErrorEnum.USER_ALREADY_EXISTS);
    }
}
